package edu.nf.bs.controller;

import edu.nf.bs.dao.AdminMapper;
import edu.nf.bs.dao.impl.AdminMapperImpl;
import edu.nf.bs.entity.Admin;
import edu.nf.bs.exception.LoginException;
import edu.nf.bs.service.impl.AdminServiceImpl;
import edu.nf.bs.vo.ResultVO;
import org.mvc.web.annotation.RequestMapping;
import org.mvc.web.servlet.View;
import org.mvc.web.view.JsonView;

import javax.servlet.http.HttpSession;

public class AdminContraller extends BaseContraller{
    @RequestMapping("/admin/login")
    public View login(Admin admin, HttpSession session){
        AdminMapper mapper=new AdminMapperImpl();
        AdminServiceImpl service=new AdminServiceImpl();
        service.setAdminMapper(mapper);
        try {
            Admin admin1 = service.checkAdminHost(admin);
            //将用户保存到session
            session.setAttribute("admin1", admin1);
            Admin admin2 =(Admin) session.getAttribute("admin1");
            //返回成功的ResultVO
            ResultVO successVo = success(admin2);
            return new JsonView(successVo);
        } catch (LoginException e) {
            e.printStackTrace();
            //响应错误内容的vo
            ResultVO failVo = fail(401, e.getMessage());
            return new JsonView(failVo);
        }
    }
}
